<?php

namespace app\service;

use app\model\MenuModel;
use app\Request;

class MenuService
{
    // 添加
    public function addMenu($name,$route,$parenId=null){

        $data=[
            'parent_id'=>$parenId,
            'name'=>$name,
            'route'=>$route,
            'create_time'=>date('Y-m-d H:i:s'),
            'update_time'=>date('Y-m-d H:i:s')
        ];
        return MenuModel::addMenu($data);
    }

    // 修改
    public function updateMenu(Request $request){
        $id = $request->all('id');
        $name = $request->all('name');
        $route = $request->all('route');
        $parenId = $request->all('parentId');
        $data=[
            'name'=>$name,
            'route'=>$route,
            'parent_id'=>$parenId,
            'update_time'=>date('Y-m-d H:i:s')
        ];
        return MenuModel::updateMenu($id,$data);
    }

    // 删除
    public function deleteMenu(Request $request){
        $id = $request->all('id');

        $data=[
            'is_del'=>'1'
        ];
        return MenuModel::updateMenu($id,$data);
    }

    public function buildMenuTree($data,$parentId=0){
        $tree = array();
        foreach ($data as $item) {
            if ($item['parent_id'] == $parentId) {
                $children = $this->buildMenuTree($data, $item['id']);
                if ($children) {
                    $item['children'] = $children;
                }
                $tree[] = $item;
            }
        }
        return $tree;
    }




}